package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.Timer;

import server.FirewallException;

import controller.FirewallSession;

/**
 * Class is responsible for system flags dynamic update.
 */
public class StatusUpdateDynamicAction implements ActionListener
{
	/**
	 * Timer for logger dynamic update
	 */
	public static Timer timer = null;
	
	/**
	 * server controller
	 */
	private FirewallSession UI = null;
	
	/**
	 * C'tor - initialize the timer and starts its work.
	 */
	public StatusUpdateDynamicAction(FirewallSession UI)
	{
		timer = new Timer(10000, this);
		this.UI = UI;
		timer.start();
	}

	/**
	 * Stops timer work.
	 */
	public void stop()
	{
		timer.stop();
	}
	
	/**
	 * For each timer tick update the system characteristics.
	 */
	public void actionPerformed(ActionEvent event)
	{
		try {
			/* Update FW status - status bar and the main menu turn on/off button */
			if(UI.isOn())
			{
				MainFrame.onoff.setText("FW Off");
				MainFrame.onoff.setToolTipText("Turn off the FW");
				StatusBar.fwTurnedOn = true;
			}
			else
			{
				MainFrame.onoff.setText("FW On");
				MainFrame.onoff.setToolTipText("Turn on the FW");
				StatusBar.fwTurnedOn = false;
			}
			
			/* Update logger status - status bar and the logger turn on/off button */
			if(UI.isLogEnabled())
			{
				StatusBar.loggerOn = true;			
				if(LoggerFrame.logonoff != null)
				{
					LoggerFrame.logonoff.setText("Logger off");
					LoggerFrame.logonoff.setToolTipText("Turn off the logger");					
				}
			}
			else
			{
				StatusBar.loggerOn = false;
				if(LoggerFrame.logonoff != null)
				{
					LoggerFrame.logonoff.setText("Logger on");
					LoggerFrame.logonoff.setToolTipText("Turn on the logger");				
				}
			}
			
			/* Rebuild the sttaus bar */
			StatusBar.update();
		} catch (RemoteException e){
			ErrorFrame.AddError("Connection problem: status bar can't be updated", null);
		} catch (FirewallException e){
			ErrorFrame.AddError("Connection problem: status bar can't be updated", null);
		}
	}
}
